mui.plusReady(function() {
	var self = plus.webview.currentWebview();
	if(self.parameter) {
		$(".mui-title").text("选择地址")
	}
})

mui.init({
	swipeBack: true,
	pullRefresh: {
		container: '#pullrefresh',
		up: {
			contentrefresh: '正在加载...',
			callback: pullupRefresh
		}

	}
});
var address = new Vue({
	el: '.address',
	data: {
		addlist: [],
		page: 1,
		swith: false
	},
	methods: {
		//选择默认
		radios: function(e) {
			if(e.isDefault == 1) {
				return
			}
			common.ajax('/userAddress/defaultaddress/' + e.addId, {}, function(data) {
				if(data.code == 1) {
					for(var i = 0; i < address.addlist.length; i++) {
						if(e.addId == address.addlist[i].addId) {
							e.isDefault = 1
//							address.addlist.splice(i, 1)
						} else {
							address.addlist[i].isDefault = 0
						}
					}
//					address.addlist.unshift(e)
					var list = plus.webview.getWebviewById("../my/userinfo.html")
					mui.fire(list, 'refurbish');
					var goBuy = plus.webview.getWebviewById('../order/confirmorder.html');
					mui.fire(goBuy, 'goBuy');
				} else {
					if(data.resultMsg) {
						common.toast(data.resultMsg)
					} else {
						console.log(data)
					}
				}
			})
		},
		//		删除
		removeRess: function(e, index) {
			var btnArray = ['是', '否'];
			mui.confirm('您确定删除该地址吗？', '', btnArray, function(success) {
				if(success.index == 0) {
					common.ajax('/userAddress/deladdress/' + e.addId, {}, function(data) {
						if(data.code == 1) {
							address.addlist.splice(index, 1)
							if(e.isDefault == 1) {
								var list = plus.webview.getWebviewById("../my/userinfo.html")
								mui.fire(list, 'refurbish');
							}
							if(address.addlist.length <= 0) {
								$('.mui-pull-bottom-pocket').css('display', 'none !important')
							} else {
								$('.mui-pull-bottom-pocket').css('display', 'block !important')
							}
							var goBuy = plus.webview.getWebviewById('../order/confirmorder.html');
							mui.fire(goBuy, 'goBuy');
						} else {
							if(data.resultMsg) {
								common.toast(data.resultMsg)
							} else {
								console.log(data)
							}
						}
					})

				}
			})

		},
		//修改
		modify: function(e) {
			console.log(e)
			common.open('addsite.html', 'addsite.html', e)
		},
		addressCheck: function(e) {
			if(plus.webview.getWebviewById('../order/confirmorder.html')) {
				//				var list = plus.webview.getWebviewById('../order/confirmorder.html');
				//				mui.fire(list, 'update', { //触发自定义事件
				//					addlist: addlist
				//				});

				common.ajax('/userAddress/defaultaddress/' + e.addId, {}, function(data) {
					if(data.code == 1) {
						for(var i = 0; i < address.addlist.length; i++) {
							if(e.addId == address.addlist[i].addId) {
								e.isDefault = 1
								address.addlist.splice(i, 1)
							} else {
								address.addlist[i].isDefault = 0
							}
						}
						address.addlist.unshift(e)
						var goBuy = plus.webview.getWebviewById('../order/confirmorder.html');
						mui.fire(goBuy, 'goBuy');
						mui.back()
					}
				},true)
			}
		}
	}
})

function pullupRefresh() {
	common.ajax('/userAddress/getList', {
		curPageNO: address.page++
	}, function(data) {
		if(data.code == 1) {
			if(data.data.pageCount < address.page) {
				//没有下一页
				mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
			} else {
				mui('#pullrefresh').pullRefresh().endPullupToRefresh()
			}
			address.swith = true
			address.addlist = address.addlist.concat(data.data.list)
			if(address.addlist.length <= 0) {
				$(".mui-pull").hide()
			} else {
				$(".mui-pull").show()
			}

		} else {
			if(data.resultMsg) {
				common.toast(data.resultMsg)
			} else {
				console.log(data)
			}
		}
	}, true, 'get')

}

if(mui.os.plus) {
	mui.plusReady(function() {
		setTimeout(function() {
			mui('#pullrefresh').pullRefresh().pullupLoading();
		}, 10);
	});
} else {
	mui.ready(function() {
		mui('#pullrefresh').pullRefresh().pullupLoading();
	});
}